"""
线程同步-Lock、RLock（字节码 dis）

电商网站开发（库存问题）

锁的问题：
    1.影响性能（获取锁和释放锁都需要时间）
    2.会引起死锁（重复获取锁、相互等待）

相互等待：
    A（a, b）
    acquire (a)
    acquire (b)

    B（a, b）
    acquire (b)
    acquire (a)
"""

import threading
from threading import Lock

total = 0

lock = Lock()


def add():
    global total
    global lock
    for i in range(100000):
        # 获取锁
        lock.acquire()
        total += 1
        # 释放锁
        lock.release()


def desc():
    global total
    global lock
    for i in range(100000):
        # 获取锁
        lock.acquire()
        total -= 1
        # 释放锁
        lock.release()


def use_thread():
    t1 = threading.Thread(target=add)
    t2 = threading.Thread(target=desc)

    t1.start()
    t2.start()

    # 阻塞
    t1.join()
    t2.join()

    print(total)
    # 输出结果：
    """
    0
    """


if __name__ == '__main__':
    use_thread()
